package com.restphp.java.hashdb.dcs;

import com.restphp.java.hashdb.core.HashDbCore;
import com.restphp.java.hashdb.dcs.client.HashDbClusterClient;
import com.restphp.java.hashdb.dcs.po.ClusterNode;

/**
 * 群集删除键值.
 * Created by sofical on 2020/8/11.
 */
public class DeleteFromCluster {
    private HashDbClusterConfig clusterConfig = null;
    public DeleteFromCluster(HashDbClusterConfig clusterConfig) {
        this.clusterConfig = clusterConfig;
    }

    /**
     * 删除键值.
     * @param root 数据落地文件址.
     * @param table 表.
     * @param key 键名.
     */
    public void delete (String root, String table, String key) {
        Long time = System.nanoTime();
        //非群集模式
        if (null == clusterConfig) {
            HashDbCore.delete(root, table, key, time);
            return;
        }

        //通知群集设置值
        HashDbClusterClient clusterClient = new HashDbClusterClient();
        for (ClusterNode node : clusterConfig.nodeList()) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    clusterClient.delete(node, root, table, key, time);
                }
            }).start();
        }
    }
}
